package com.flyiu.data.face;

import static java.util.stream.Collectors.joining;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
 * 数组MAP 与 字符串转换
 * 
 * @author chenjh
 *
 */
public class ArrayMapLoadStore {

	/**
	 * 生成测试数据
	 * 
	 * @return
	 */
	private Map<String, String>[] initList() {
		List<Map<String, String>> textList = new ArrayList<>();
		Map<String, String>[] mapArrays = new HashMap[2];

		Map<String, String> map1 = new HashMap<>();
		map1.put("key1", "value1");
		map1.put("key2", "value2");

		Map<String, String> map2 = new HashMap<>();

		map2.put("keyA", "valueA");

		textList.add(map1);
		textList.add(map2);

		mapArrays[0] = map1;
		mapArrays[1] = map2;
		return mapArrays;
	}

	/**
	 * 将字符串转换为map ,输入格式为：</br>
	 * key1=value1;key2=value2。entry之间以;分割
	 * 
	 * @param mapText
	 *            示例为 key1=value1;key2=value2
	 * @return
	 */
	private Map<String, String> parseToMap(String mapText) {
		Map<String, String> map = new HashMap<>();
		String[] mapArray = mapText.split(";");
		for (String mapEntryString : mapArray) {
			String[] entry = mapEntryString.split("=");
			map.put(entry[0].trim(), entry[1].trim());
		}
		return map;
	}

	/**
	 * 把文本字符串中的内容读取为数组
	 * 
	 * @param text
	 *            格式为 key1=value1;key2=value2\nkeyA=valueA </br>
	 *            其中\n区分多个map, 以;符号区分entry
	 * @return
	 */
	public Map<String, String>[] load(String text) {
		Assert.isTrue(StringUtils.isNoneBlank(text), "非法入参");
		String[] mapTextArray = text.split("\\\\n");

		Map<String, String>[] mapArrays = new HashMap[mapTextArray.length];

		for (int i = 0; i < mapTextArray.length; i++) {
			String mapText = mapTextArray[i];
			mapArrays[i] = parseToMap(mapText);
		}
		return mapArrays;
	}

	/**
	 * 非第第一个map其后加\n
	 * 
	 * @param i
	 * @param text
	 */
	private void needAppendNewLine(int i, StringBuilder text) {
		if (i > 0) {
			text.append("\\n");
		}
	}

	/**
	 * 把数组解析成一个文本字符串
	 * 
	 * @param initArray
	 * @return
	 */
	public String store(Map<String, String>[] initArray) {
		StringBuilder text = new StringBuilder();
		for (int i = 0; i < initArray.length; i++) {
			Map<String, String> map = initArray[i];
			needAppendNewLine(i, text);

			String mapText = map.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue())
					.collect(joining(";"));
			text.append(mapText);
		}
		return text.toString();

	}

	public static void main(String[] args) {
		ArrayMapLoadStore arrayMapDurability = new ArrayMapLoadStore();
		String text = arrayMapDurability.store(arrayMapDurability.initList());
		System.out.println(text);
		Map<String, String>[] descMapArray = arrayMapDurability.load(text);

		String text2 = arrayMapDurability.store(descMapArray);
		System.out.println(text2);
		Assert.isTrue(StringUtils.equals(text, text2), "验证失败");
	}

}
